/**
 * 
 */
package org.bankabeule.ui.wizards;

import org.bankabeule.Account;
import org.bankabeule.core.ModelManager;
import org.bankabeule.ui.utils.Logger;
import org.bankabeule.util.Utils;
import org.eclipse.jface.wizard.Wizard;

/**
 * @author sdudoit
 *
 */
public class AccountWizard extends Wizard {

	
	/**
	 * Account
	 */
	private Account account;
	
	/**
	 * Account page
	 */
	private AccountPage page;
	
	
	/**
	 * Constructor
	 */
	public AccountWizard(Account existingAccount) {
		account = existingAccount;
		setWindowTitle("Edit account");
	}
	
	
    /**
     * Gets the federate.
     */
    public void addPages() {
    	page = new AccountPage("AccountPage", account);
        addPage(page);
    }
    
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public boolean performFinish() {
		try {
			if (account == null) {
				// new account to add
				account = page.getAccount();
				Utils.calculateValue(account);
				ModelManager.get().getModel().getAccounts().add(account);
			} else {
				// update account
				account.setName(page.getAccount().getName());
				account.setBankid(page.getAccount().getBankid());
				account.setBranchid(page.getAccount().getBranchid());
				account.setType(page.getAccount().getType());
				account.setInitValue(page.getAccount().getInitValue());
				Utils.calculateValue(account);
				account.setMoney(page.getAccount().getMoney());
				account.setDescription(page.getAccount().getDescription());
			}
			return true;
		} catch (Exception e) {
			Logger.log(e);
			return false;
		}
	}

}
